module Queue.SimpleQueue (Queue,
                    isEmpty,
                    empty,
                    add,
                    head,
                    tail) where

import Prelude hiding (head, tail)

data Queue a = Queue{left::[a], right::[a]}

isEmpty Queue{left=[]} = True
isEmpty _ = False

empty = Queue [] []

add q@Queue{right=xs} x = norm q{right=x:xs}

head Queue{left=x:_} = x
head Queue{left=[]} = error "head on empty queue"

tail q@Queue{left=_:xs} = norm q{left=xs}
tail Queue{left=[]} = error "tail on empty queue"

norm q@Queue{left=xs,right=ys} =
  if length ys > length xs then
    q{left=xs++reverse ys, right=[]}
  else q
